package com.example.hbase.handler;

import com.example.hbase.handler.data.*;
import com.example.hbase.handler.ext.ListHandler;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 默认类型处理器
 */
public class DefaultTypeHandlers {
    private static EnumHandler enumHandler     = new EnumHandler();
    private static Map<Class<?>, TypeHandler> defaultHandlers = new HashMap<>();

    static {
        defaultHandlers.put(String.class, new StringHandler());
        defaultHandlers.put(Date.class, new DateHandler());

        defaultHandlers.put(Boolean.class, new BooleanHandler());

        defaultHandlers.put(Character.class, new CharacterHandler());

        defaultHandlers.put(Byte.class, new ByteHandler());
        defaultHandlers.put(Short.class, new ShortHandler());
        defaultHandlers.put(Integer.class, new IntegerHandler());
        defaultHandlers.put(Long.class, new LongHandler());
        defaultHandlers.put(Float.class, new FloatHandler());
        defaultHandlers.put(Double.class, new DoubleHandler());

        defaultHandlers.put(BigDecimal.class, new BigDecimalHandler());
        defaultHandlers.put(List.class, new ListHandler());
    }

    /**
     * 寻找对应类型的处理器
     * @param type
     * @return
     */
    public static TypeHandler findDefaultHandler(Class<?> type) {
        Assert.notNull(type, "数据类型不能为空");

        if (type.isEnum()) {
            return enumHandler;
        }

        return defaultHandlers.get(type);
    }
}
